package com.ys.controller.web.hr;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ys.config.MenuId;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.hr.ThrOperateCompanyBeianService;
import com.ys.util.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bo.vo.W;
import com.ys.entity.hr.ThrOperateCompanyBeian;
import com.ys.service.sys.CommonApiService;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/web/hr/thrOperateCompanyBeian")
public class WebThrOperateCompanyBeianController {
	private static final Logger logger = LoggerFactory.getLogger(WebThrOperateCompanyBeianController.class);
	
	@Autowired
	private CommonApiService<ThrOperateCompanyBeian> commonApiService;

	@Autowired
	private ThrOperateCompanyBeianService thrOperateCompanyBeianService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("thrOperateCompanyBeian:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		ThrOperateCompanyBeian entity = new ThrOperateCompanyBeian();
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.THR_OperateCompanyBeian);
			entity.setBid(bid);
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("code", "保存后自动生成");
			extraParam.put("startPerformance", 85);//运营初始绩效
			return commonApiService.toAdd(model, entity, MenuId.THR_OperateCompanyBeian, extraParam,null);
		} catch (Exception e) {
			logger.error("运营公司备案新增页面出错：",e.getMessage(),e);
			return com.bo.vo.W.error(model, e.getMessage());
		}
	}


	/*
	 * 新增-关联招商信息申请单
	 */
	@RequiresPermissions("thrOperateCompanyBeian:addByCanvassApply")
	@GetMapping("/toAddByCanvassApply")
	public String toAddByCanvassApply(Model model, HttpServletRequest request, HttpServletResponse response){

		ThrOperateCompanyBeian entity = new ThrOperateCompanyBeian();
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.THR_OperateCompanyBeian_By_CanvassApply);
			entity.setBid(bid);

			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("code", "保存后自动生成");
			extraParam.put("startPerformance", 85);//运营初始绩效
			extraParam.put("operationsAttribute", 2);//运营属性-默认运营账户
			model.addAttribute("onProcess", 0);
			commonApiService.toAdd(model, entity, MenuId.THR_OperateCompanyBeian_By_CanvassApply, extraParam,null);
			//return commonApiService.toAdd(model, entity, MenuId.THR_OperateCompanyBeian_By_CanvassApply, extraParam);
			return "hr/thrOperateCompanyBeian/toAdd";
		} catch (Exception e) {
			logger.error("运营公司备案新增页面出错：",e.getMessage(),e);
			return com.bo.vo.W.error(model, e.getMessage());
		}
	}

	
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,String bid){
		Map<String,Object> entity = new ThrOperateCompanyBeian()
				.addLeftJoin("TS_UserChange B on B.Id = A.OperatorChangeId")
				.addField("B.UserId operatorUserId")//运营人工号
				.addField("B.UserName operatorUserName")//运营人
				.addField("B.RoleName roleName")//运营人岗位
				.addField("B.DeptName deptName")//运营人部门

				.addLeftJoin("TS_OperateOrg O on O.Id = A.OperateOrgId")
				.addLeftJoin("THR_OperateCompanyApply PO ON O.HeadVgsbm = PO.Vgsbm and 1=1")
				.addField("O.Name orgName")//机构名称
				//.addField("O.Pid orgbm")//上级公司编码
				//.addField("PO.Name orgJc")//上级公司简称
				.addField("PO.Vgsbm AS orgbm")//上级公司编码
				.addField("PO.Vgsjc AS orgJc")//上级公司简称
				.setBid(bid)
				.queryFirstMap();

		Map<String, Object> extraParam = new HashMap<String, Object>();
		//关联数据
		if(entity.get("operatorUserId")!=null)extraParam.put("operatorUserId", entity.get("operatorUserId"));
		if(entity.get("operatorUserName")!=null)extraParam.put("operatorUserName", entity.get("operatorUserName"));
		if(entity.get("roleName")!=null)extraParam.put("roleName", entity.get("roleName"));
		if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
		if(entity.get("orgName")!=null)extraParam.put("orgName", entity.get("orgName"));
		if(entity.get("orgbm")!=null)extraParam.put("orgbm", entity.get("orgbm"));
		if(entity.get("orgJc")!=null)extraParam.put("orgJc", entity.get("orgJc"));
		if(entity.get("pbid")!=null)extraParam.put("pbid", entity.get("pbid"));
		if(entity.get("investType")!=null)extraParam.put("investType", entity.get("investType"));
		if(entity.get("costStandard")!=null)extraParam.put("costStandard", entity.get("costStandard"));

		try {
			if (entity.get("canvassApplyItemId") != null && !"".equals(entity.get("canvassApplyItemId"))) {
				//获取审核时间
				YsProcInst ysProcInst = new YsProcInst().setBid(entity.get("bid").toString()).queryFirst();
				if(ysProcInst!=null && ysProcInst.getUpdateTime()!=null)extraParam.put("auditTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));
				Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
				model.addAttribute("onProcess", onProcess);
				ThrOperateCompanyBeianEdit detailList = new ThrOperateCompanyBeianEdit(
						Long.parseLong(entity.get("canvassApplyItemId").toString()),
						entity.get("businesslicense") != null ? entity.get("businesslicense").toString() : "",
						entity.get("cooperateAgreement") != null ? entity.get("cooperateAgreement").toString() : "",
						entity.get("investType") != null ? entity.get("investType").toString() : "");
				model.addAttribute("detail", detailList);
				commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new ThrOperateCompanyBeian(), MenuId.THR_OperateCompanyBeian_By_CanvassApply, extraParam,null);
				return "hr/thrOperateCompanyBeian/toEdit";
			} else {
				String[] bidTimeArr = bid.split("-");
				String bidTime = bidTimeArr[1];
				Long menuId = MenuId.THR_OperateCompanyBeian;
				int result = bidTime.compareTo("240501");
				if(result < 0){
					menuId = MenuId.THR_OperateCompanyBeian_Old;
					model.addAttribute("onProcess", 1);
					commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new ThrOperateCompanyBeian(), menuId, extraParam,null);
					return "hr/thrOperateCompanyBeian/toOld";
				}else{
					//获取审核时间
					YsProcInst ysProcInst = new YsProcInst().setBid(entity.get("bid").toString()).queryFirst();
					if(ysProcInst!=null && ysProcInst.getUpdateTime()!=null)extraParam.put("auditTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));
					Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
					model.addAttribute("onProcess", onProcess);
				}
				return commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new ThrOperateCompanyBeian(), menuId, extraParam,null);
			}
		}catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEditByCanvassApply")
	public String toEditByCanvassApply(Model model, HttpServletRequest request, HttpServletResponse response,String bid){

		try {
			Map<String,Object> extraParam = thrOperateCompanyBeianService.getExtraParam(bid);

			//获取审核时间
			YsProcInst ysProcInst = new YsProcInst().setBid(extraParam.get("bid").toString()).queryFirst();
			if(ysProcInst!=null && ysProcInst.getUpdateTime()!=null)extraParam.put("auditingTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));
			Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
			model.addAttribute("onProcess", onProcess);


			ThrOperateCompanyBeianEdit detailList = new ThrOperateCompanyBeianEdit(
					Long.parseLong(extraParam.get("canvassApplyItemId").toString()),
					extraParam.get("businesslicense")!=null?extraParam.get("businesslicense").toString():"",
					extraParam.get("cooperateAgreement")!=null?extraParam.get("cooperateAgreement").toString():"",
					extraParam.get("investType")!=null?extraParam.get("investType").toString():"");
			model.addAttribute("detail", detailList);
			commonApiService.toEdit(model, Long.parseLong(extraParam.get("id").toString()), new ThrOperateCompanyBeian(), MenuId.THR_OperateCompanyBeian_By_CanvassApply, extraParam,null);
			return "hr/thrOperateCompanyBeian/toEdit";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}


	@Data
	@AllArgsConstructor
	private static class ThrOperateCompanyBeianEdit {
		private Long canvassApplyItemId;
		private String businesslicense;
		private String cooperateAgreement;
		private String investType;
	}
}
